<template>
	<view class="content">
		<u-sticky>
			<!-- 只能有一个根元素 -->
			<view class="sticky">
				<u-search focus shape="square" @change="handleSearchValue" placeholder="请输入专业名称" v-model="keyword"
					@custom="handleSearchValue" @search="handleSearchValue"></u-search>
			</view>
		</u-sticky>
		<view v-if="list && list.length">
			<view @click="chooseItem(item)" class="item" v-for="(item,index) in list" :key="item.id">
				{{item.majorCode + ' - ' + item.majorName}}
			</view>
		</view>
		<view v-if="(!list || !list.length) && isOne">
			<uni-nodata></uni-nodata>
		</view>
	</view>
</template>

<script>
	import {
		getzhuanye,
	} from "@/api/toolbox.js"
	export default {
		data() {
			return {
				isOne: false,
				keyword: "",
				list: [],
				schoolCode: null,
				allList: [],
			}
		},
		onLoad(e) {
			if (e.schoolCode) {
				this.schoolCode = e.schoolCode;
				this.getData();
			}
		},
		methods: {
			//获取专业数据
			getData() {
				getzhuanye({
					schoolCode: this.schoolCode
				}).then(res => {
					this.isOne = true;
					let list = res.data.map(e => {
						return {
							schoolCode: e.schoolCode,
							schoolName: e.schoolName,
							majorName: e.majorName,
							majorCode: e.majorCode,
							id: e.id
						}
					})
					this.allList = JSON.parse(JSON.stringify(list));
					this.list = JSON.parse(JSON.stringify(list));
				})
			},
			//点击选中
			chooseItem(item) {
				uni.setStorageSync('chooseMajorObj', {
					'majorName': item.majorName,
					'majorCode': item.majorCode
				});
				uni.navigateBack(1);
			},
			//进行搜索
			handleSearchValue() {
				if (this.keyword) {
					let list = this.allList.filter(e => {
						if (e.majorName.indexOf(this.keyword) > -1) {
							return e
						}
					})
					this.list = JSON.parse(JSON.stringify(list));
				} else {
					this.list = JSON.parse(JSON.stringify(this.allList));
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 0 20rpx;
	}

	.item {
		border-bottom: 2rpx solid #e6e6ea;
		padding: 20rpx 10rpx;
	}
</style>